package com.security.check;

import com.common.exception.ImageCodeIllegalException;
import com.common.exception.ImageTimeOutException;
import com.security.auth.Role;
import com.security.auth.User;
import com.security.auth.UserDetail;
import com.security.dao.SecurityUserDao;
import com.security.entity.CustomWebAuthenticationDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component()
public class CustomAuthenticationProvider implements AuthenticationProvider {

    public CustomAuthenticationProvider(){
        super();
    }

    @Autowired
    private SecurityUserDao securityUserDao;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        //输入的用户名
        String username = authentication.getName();
        //输入的密码
        String password = (String) authentication.getCredentials();
        //验证码信息
//        CustomWebAuthenticationDetails details = (CustomWebAuthenticationDetails) authentication.getDetails();
//        String imageCode = details.getImageCode();
//        String session_imageCode = details.getSession_imageCode();
//        long session_imageTime = details.getSession_imageTime();

        User userEntity = securityUserDao.getUserByName(username);
        if (userEntity == null){
            throw new UsernameNotFoundException(username+"用户不存在！");
        }

        /**校验密码*/
        if(!"123456".equals(password)){
            throw new UsernameNotFoundException("密码错误！");
        }

//        if(imageCode == null || session_imageCode == null) {
//            throw new ImageCodeIllegalException("验证码错误");
//        }
//
//        if(!imageCode.toUpperCase().equals(session_imageCode.toUpperCase())) {
//            throw new ImageCodeIllegalException("验证码错误");
//        }else{
//            long nowTime = System.currentTimeMillis();
//            if((nowTime - session_imageTime)/1000 > 60000) { //大于60000s,超时
//                throw new ImageTimeOutException("验证码已超时");
//            }
//        }

        List<SimpleGrantedAuthority> simpleGrantedAuthorities = new ArrayList<SimpleGrantedAuthority>();
//        List<RoleEntity> sysRoleEntityList = sysRoleEntityMapper.selectByUserId(userEntity.getId());
//        if(sysRoleEntityList!=null&&sysRoleEntityList.size()>0){
//            final List<GrantedAuthority> grantedAuths = new ArrayList<>();
//            Iterator<RoleEntity> sysRoleEntityIterator = sysRoleEntityList.iterator();
//            while (sysRoleEntityIterator.hasNext()){
//                RoleEntity sysRoleEntity = sysRoleEntityIterator.next();
//                simpleGrantedAuthorities.add(new SimpleGrantedAuthority(String.valueOf(sysRoleEntity.getId())));
//            }
//            final UserDetails principal = new User(username, password, grantedAuths);
//            final Authentication auth = new UsernamePasswordAuthenticationToken(principal, password, simpleGrantedAuthorities);
//            return auth;
//        }

        final List<GrantedAuthority> grantedAuths = new ArrayList<>();
        simpleGrantedAuthorities.add(new SimpleGrantedAuthority(String.valueOf("qq")));
        final UserDetails principal = new UserDetail(userEntity.getId(),username, new Role("qq"));
        final Authentication auth = new UsernamePasswordAuthenticationToken(principal, password);
        return auth;

    }

    @Override
    public boolean supports(Class<?> authentication) {
        return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));

    }
}
